﻿using System;

namespace SilverlightTetris_M10CDE.Web
{
    /// <summary>
    /// Manages the blocks of docked shapes and checks if shapes can be docked
    /// </summary>
    public class GameField
    {
        Block[,] blockField;//2D array for holding docked blocks

        private int score;//holds the current score
        private int clearedLines;//total no. of lines cleared
        private bool bonus;//used in updateScore()

        /// <summary>
        /// Constructor
        /// </summary>
        public GameField()
        {
            blockField = new Block[15, 18];
            score = 0;
        }

        /// <summary>
        /// Adds a block to the blockField array
        /// </summary>
        /// <param name="myBlock"></param>
        public void dockBlock(Block myBlock)
        {
            if (checkCoordinates(myBlock.xPosition, myBlock.yPosition))
            {
                blockField[myBlock.xPosition, myBlock.yPosition] = myBlock;
            }
        }

        /// <summary>
        /// Checks blockField array (using X and Y to address an element) to see if element is null or occupied
        /// </summary>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <returns>boolean value</returns>
        public bool checkCoordinates(int X, int Y)
        {

            if (X < 0 || X >= 15 || Y < 0 || Y > 23 || blockField[X, Y] != null)
            {
                return false;//returns false if array index is out of bounds or occupied
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// clears a row from the blockField 2Darray and copies above rows down
        /// </summary>
        /// <param name="line"></param>
        public void removeLine(int line)
        {
            int row;
            for (int a = 0; a < 15; a++)//'a' refers to the X position (columns)
            {
                blockField[a, line] = null;//destroys the reference
                row = line;
                for (int b = (line - 1); b >= 0; b--)//moves the blocks above the target row down by one
                {
                    blockField[a, row] = blockField[a, b];//updates the blockField array
                    row--;
                }
            }
        }

        /// <summary>
        /// Calculates and returns the latest score based on the number of lines cleared by a docked shape
        /// </summary>
        /// <param name="scoreMultiplier"></param>
        /// <returns>score</returns>
        /*
            Awards 100 points for each line cleared. 
            If 4 lines are cleared at once (a Tetris) points are doubled.
            If 2 consecutive Tetris are acheived, an additional 800 points are awarded.
        */
        public int updateScore(int scoreMultiplier)
        {
            if (scoreMultiplier == 4)
            {
                scoreMultiplier = 8;
                if (bonus)
                {
                    scoreMultiplier = 16;
                }
                bonus = true;
            }
            else
            {
                bonus = false;
            }
            score += scoreMultiplier * 100;
            return score;
        }

        /// <summary>
        /// Updates and returns the total number of cleared lines
        /// </summary>
        /// <param name="lines"></param>
        /// <returns>clearedLines</returns>
        public int updateLines(int lines)
        {
            clearedLines += lines;
            return clearedLines;
        }
    }
}